package com.zfx.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfx.pojo.User;
import com.zfx.service.IUserService;
import com.zfx.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: zfx
 * @version: 1.0
 * @time: 2020-05-09 12:08
 * <p>
 * 修改历史：
 * 修改日期      修改人员    版本    内容
 * 2020-05-09   zfx       1.0
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public JsonObject getList(@RequestBody String jsonData) throws IOException {
        JsonObject jsonObject = new JsonObject();
        ObjectMapper objectMapper = new ObjectMapper();

        System.out.println(jsonData);

        HashMap<String, Object> dataMap = objectMapper.readValue(jsonData, HashMap.class);

        User user = new User(dataMap);

        System.out.println(user);

        jsonObject.setData("userList", userService.findByUser(user, (int) dataMap.get("page"), (int) dataMap.get("limit")));
        jsonObject.setDataSize(userService.countByUser(user));
        jsonObject.setMsg("success");
        jsonObject.setCode("200");

        return jsonObject;
    }
}
